<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <script type="text/javascript" src="../dashboard/javascript/jquery/jquery.tools.min.js"></script>  
    <script type="text/javascript">  //<![CDATA[    

      /** - set up event handlers  --------------------------------------- */
      $(document).ready(function() {
          $("#foo").click(function() {
              console.debug("Running tests"); 
              run_tests();
            });
        });


      /** - global error handler for Ajax requests  ----------------------- */
      $(document).ajaxError(function(ajax, response, request) {
          console.error("AJAX:     " + ajax.type);
          console.error("RESPONSE: " + response.status + " " + response.statusText + "\n" + response.responseText);
          console.error("REQUEST:  " + request.data);
        });


      /** - add pretty printing to jQuery objects ------------------------- */
      var dump = function(dict) {
        // return JSON.stringify(dict);
        var ret = "{ ";
        for (var key in dict) {
          ret += key + ":" + dict[key] + "  ";
        }
        return ret + " }";
      };
          

      /** - JSON ------------------------------------------------------ */
      var test_json = function(host, test_data, type) {
        var url = "http://" + host + "/cgi-bin/afrimesh";
        var request = { 
          url         : url,
          type        : type,
          contentType : "application/json",
          dataType    : "json",
          async       : false,
            data        : JSON.stringify(test_data),
            success     : function (result) {
              console.debug("JSON result: " + JSON.stringify(result));
            } 
        };
        var xml = $.ajax(request);
      };


      /** - JSONP ----------------------------------------------------- */
      var test_jsonp = function(host, test_data, type, callback) {
        var url = "http://" + host + "/cgi-bin/afrimesh" + (callback ? "?jsonp=" + callback : "");
        var request = { 
          url         : url,
          type        : type,
          contentType : "application/json",
          dataType    : "jsonp",
          jsonp       : callback,
          // async       : false, /* jsonp can only do async */
          data        : { payload : JSON.stringify(test_data) },
          success     : function (result) {
            console.debug("JSONP result: " + JSON.stringify(result));
          }
        };
        var xml = $.ajax(request);
      };


      /** - JSON/RPC -------------------------------------------------- */
      var rpc = function(method, parameters) {
        // TODO - check host & path
        var request = {
          url         : "http://" + rpc.host + rpc.path, 
          type        : "POST",
          contentType : "application/json",
          dataType    : "json",
          async       : false
        };
        var call = { 
          id      : 1234,
          version : "2.0",
          method  : method,
          params  : parameters
        };
        request.data = JSON.stringify(call);
        request.success = function(response, result) {
          //console.debug("GOT: " + JSON.stringify(response));
          if (response.error) {
            console.error("JSON/RPC ERROR: " + response.error);
          } else if (response.result) {
            request.result = response.result;
          }
        };
        var xml = $.ajax(request);
        return request.result;
      }; 


      /** - run the tests when the button is clicked ---------------------- */
      var run_tests = function() {
        console.debug("Making request"); 
              
        // configure rpc
        rpc.host = "127.0.0.1";
        var token = "533e0de345a95c427436f6597b3ac0bf";
        var json_error_handler = function(message) {
          console.error("JSON ERROR: " + JSON.stringify(message));
        };
        var jsonp_error_handler = function(message) {
          console.error("JSONP ERROR: " + JSON.stringify(message));
        };

        // some test data
        var test_data_1 = { key1 : "value1", key2 : "value2" };
        var test_data_2 = { key1 : "value1", key2 : { subkey1 : "subvalue1", subkey2 : "subvalue2" }, key3 : "value3" };

        // test 1 - jsonp 
        //test_json(rpc.host, test_data_2, "GET");
        //test_json(rpc.host, test_data_2, "POST");

        // test 2 - jsonp
        //test_jsonp(rpc.host, test_data_2, "GET");
        //test_jsonp(rpc.host, test_data_2, "GET", "foocallback");
        //test_jsonp(rpc.host, test_data_2, "POST");
        /* TODO - w/ JSONP & POST the 'jsonp=callbackname' parameter gets passed in the query string 
           test_jsonp(rpc.host, test_data_2, "POST", "foocallback"); */

        // test 3 - authenticate rpc
        /*rpc.host = "192.168.1.20";
        token = undefined;
        if (!token) {
          rpc.path = "/cgi-bin/luci/rpc/auth";
          //var token = rpc("login", { username : "root", password: "" }); 
          var token = rpc("login", ["root", ""]); 
          console.debug("login token: " + token);
          if (!token) return;
        }
        rpc.path = "/cgi-bin/luci/rpc/sys?auth=" + token;
        var hostname = rpc("hostname", []); 
        console.debug("hostname: " + hostname);*/


        // test 4 - json-rpc
        //rpc.host = "127.0.0.1";
        rpc.host = "196.211.3.106";

        /*rpc.path = "/cgi-bin/afrimesh/snmp?auth=" + token;
        var snmp_walk = rpc("walk", [ "192.168.20.1", "public", "1.3.6.1.2.1.1"  ]);
        console.debug("snmp walk: " + JSON.stringify(snmp_walk));
        var snmp_get = rpc("get", [ "192.168.20.1", "public", [ "1.3.6.1.2.1.2.2.1.10.1", "1.3.6.1.2.1.2.2.1.16.1" ] ]);
        console.debug("snmp get: " + JSON.stringify(snmp_get));*/

        rpc.path = "/cgi-bin/afrimesh/uci?auth=" + token;
        var uci_show = rpc("show", [ "afrimesh" ]);
        console.debug("uci show: " + JSON.stringify(uci_show));

        // test 5 - jsonp-rpc 

      };


    </script>

  </head>
  <body>

    <button id="foo">foo</button>

  </body>
</html>
